//pobieranie seeda
var loggedIn = false;
var hasSeed = false;
var seed;
var role;
var jSessionId;
var xmlHttp;

function getSeed(nextOne) {
	debug_info('getSeed','');
	if (!loggedIn && !hasSeed) {
	// teraz przygotujemy sobie dane do wyslania
	var seedArray = new Array();
	seedArray.push("task=" + encodeURIComponent("create_seed"))
	g_ajax_obj.CallXMLHTTPObjectPOSTParamPartial('users?'+seedArray.join('&'),handleGetSeed, nextOne ,handlePartialGetSeed, '');
	loaderOn();
	debug_info('getSeed','Finish');
	} else {
		debug_info('getSeed', 'Seed not needed!');
	}
	return true;
}

function handleGetSeed(xmlDoc, nextOne, param2) {
	loaderOff();
	xmlObj=xmlDoc.documentElement;
	if(!xmlValidate(xmlObj)) {
		debug_info('handleGetSeed', 'Cos z XML nie wyszlo');
		return false;
	}
	if(!xmlGetResult(xmlObj, 'taskSeed')) {
   		hasSeed = false; 
		debug_info('handleGetSeed','Wrong answer');
	} else {
		seed = xmlGetResult(xmlObj, 'taskSeed');
		hasSeed = true;
		debug_info('handleGetSeed','Answer ok');
		if (nextOne == 'performLogin')
			setTimeout("performLogin()", 100);
	}
}

function handlePartialGetSeed(input, param) {
	debug_info('handlePartialGetSeed', 'Mamy partiala! input:' + input + ' param: '+param);
}


function performLogin() {
	loaderOn();
	debug_info('performLogin','');
	if (loggedIn) return false;
	if (!hasSeed) {
		if (!getSeed("performLogin")) {
			debug_info('performLogin', 'getSeed failed');
			return false;
		};
		return true;
	}
	debug_info('performLogin','Data from input');
	//teraz sobie pobierzemy dane zeby sie nimi pobawic
	var user = document.getElementById('login_username').value;
	var password = document.getElementById('login_password').value;
	//po co przesyłać to plain textem, jak można sobie zahashować
	debug_info('performLogin','Hash data');
	var hash = hex_md5(hex_md5(password).toUpperCase() + seed).toUpperCase();
	var seedArray = new Array();
	seedArray.push("task=" + encodeURIComponent("create_session"));
	seedArray.push("username=" + user);
	seedArray.push("password=" + hash);
	hasSeed = false;
	seed = '';
	debug_info('performLogin','doAjax');
	g_ajax_obj.CallXMLHTTPObjectPOSTParamPartial('users?'+seedArray.join('&'), loginHandler, '',loginHandlerPartial, '');
	debug_info('performLogin','Finish '+ ajaxBusy);
}

function loginHandlerPartial(input, param) {
	debug_info('loginHandlerPartial', 'Mamy partiala! input:' + input + ' param: '+param);
}

function loginHandler(xmlDoc, param1, param2) {
	   debug_info('loginHandler','Answer received');
	   xmlObj=xmlDoc.documentElement;
	   if(!xmlValidate(xmlObj)) {
		loaderOff();
		return false;
	   }
	   var error = xmlGetError(xmlObj);
	   if (!error) {
	   	   var result = xmlGetLoginResult(xmlObj, 'taskLogin');
		   if(!result) {
				loggedIn = false;
				debug_info('loginHandler','Wrong answer');
			} else {
				debug_info('loginHandler','Answer ok');
				loggedIn = true;
				loginBoxHandler();
			}
		} else {
			error_info('loginHandler', error);
		}
		loaderOff();
}

function logout() {
	loaderOn();
	debug_info('logout','');
	var seedArray = new Array();
	seedArray.push("task=" + encodeURIComponent("remove_session"));
	seedArray.push("jSessionId="+ jSessionId);
	debug_info('logout','doAjax');
	g_ajax_obj.CallXMLHTTPObjectPOSTParamPartial('users?'+seedArray.join('&'), logoutHandler, '',logoutHandlerPartial, '');
	debug_info('logout','Finish');
	return true;
}

function logoutHandler(xmlDoc, param1, param2) {
  loaderOff();
  debug_info('logoutHandler','Answer received');
   xmlObj=xmlDoc.documentElement;
   if(!xmlValidate(xmlObj)){
   	return false;
   } 
   var error = xmlGetError(xmlObj);
   if (!error) {
	   var result = xmlGetResult(xmlObj, 'taskLogout');
	   if(!result) {
			loggedIn = true;
			debug_info('logoutHandler','Wrong answer');
		} else {
			debug_info('logoutHandler','Answer ok');
			if (result == 'session_removed') {
			debug_info('logoutHandler','User logged out');
			disableLoginLink();
			loggedIn = false;
			jSessionId = "";
			role = "";
			enableLoginLink();
			}
		}
	} else {
		error_info('logoutHandler', error);
	}
}

function logoutHandlerPartial(input, param) {
	debug_info('logoutHandlerPartial', 'Mamy partiala! input:' + input + ' param: '+param);
}

function xmlGetLoginResult(xmlDoc) {
		try {
		if(xmlObj.childNodes[1].textContent == 'taskLogin' 
			&& xmlObj.childNodes[3].textContent == 'jSessionId' 
			&& xmlObj.childNodes[5].textContent == 'role'){
			jSessionId =  xmlObj.childNodes[4].textContent;
			role = xmlObj.childNodes[6].textContent;
			return xmlObj.childNodes[2].textContent;
		}
	} catch (ex){
		error_info('xmlGetResult error');
	}
	return false;
}